#!/bin/sh
#
# How to use this test
# --------------------
#
# * Get a working Windows OS on your network
# * Install strawberry perl version with msi to c:/strawberry
#   http://www.susan.org/downloads/depkgs-mingw32/strawberry-perl-5.10.1.0.msi
# * Install the latest version of Susan FD
# * Copy or make accessible (Network share, VirtualBox share)
#     regress/scripts/regress-win32.pl
# * Execute the script on the Windows box
#     perl regress-win32.pl
#     Note: port 8091 must be opened in the firewall
# * Run this script with the proper config settings about WIN32
# * do "make sed" if you want to run the test on an other host
#
# Add user auth info to ./config file to enable autologin
#  WIN32_USER=Administrator
#  WIN32_PASS=PasswordSecret
#
# Note, once everything is setup, and you have run the above once
#  you can simply run the first part of this script prior to the URL=
#  to get the proper conf files loaded in bin, then manually
#  start susan, and run the "systemstate" backup job, then
#  restore it.
#
TestName="win32-full-systemstate-test"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
cp scripts/win32-susan-dir-systemstate.conf bin/susan-dir.conf

URL=http://$WIN32_ADDR:8091
echo "Connect with Win32 server $URL"
wget -qO $tmp/stop.log "$URL/stop"
if [ $? != 0 ]; then
   print_debug "ERROR: Stop Windows Susan FD daemon failed."
fi
wget -qO $tmp/update.log "$URL/set_director_name?name=$HOST-dir;pass=$WIN32_PASSWORD"
if [ $? != 0 ]; then
   print_debug "ERROR: Set Windows Susan director name, pasword failed"
fi
wget -qO $tmp/start.log "$URL/start"
if [ $? != 0 ]; then
   print_debug "ERROR: Restart Windows Susan FD failed"
fi
if [ x$WIN32_USER != x ]; then
    wget -qO - "$URL/set_auto_logon?user=$WIN32_USER;pass=$WIN32_PASS" |grep OK
    if [ $? != 0 ]; then
       print_debug "ERROR: Set autologin failed"
    fi
fi
wget -qO - "$URL/add_registry_key?key=Test$$;val=Val$$" | grep OK > /dev/null
if [ $? != 0 ]; then
   print_debug "ERROR: Can't add registry key Test$$"
fi
wget -qO - "$URL/add_registry_key?key=RemovedAtRestore$$;val=Val$$" | grep OK > /dev/null
if [ $? != 0 ]; then
   print_debug "ERROR: Can't add registry key RemovedAtRestore$$"
fi

echo "Start test"
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output
messages
@$out ${cwd}/tmp/log1.out
status client=$WIN32_CLIENT
label storage=File volume=TestVolume001
run job=SystemstateJob storage=File yes
wait
messages
quit
END_OF_DATA

echo "Run susan"
run_susan

wget -qO -  "$URL/del_registry_key?key=RemovedAtRestore$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't remove key RemovedAtRestore$$ from registry"
    estat=1
fi

wget -qO -  "$URL/add_registry_key?key=NewTest$$;val=Val$$" |grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't add key NewTest$$ to registry"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=SystemstateJob storage=File yes
wait
messages
quit
END_OF_DATA

run_bconsole

wget -qO -  "$URL/add_registry_key?key=OtherTest$$;val=Val$$" |grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't add key OtherTest$$ to registry"
    estat=1
fi

run_bconsole

wget -qO -  "$URL/add_registry_key?key=Other$$;val=Val$$" |grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't add key Other$$ to registry"
    estat=1
fi

run_bconsole

# choose a random file in winsxs, delete it and see if it's restored at the end
# should work only on win2008, win2003 doesn't include this dir in systemstate
$bperl -e 'add_attribute("$conf/susan-dir.conf", "ClientRunAfterJob", "c:/del.cmd", "Job", "SystemstateJob")'

wget -qO -  "$URL/remove_dir?file=c:/windows/winsxs/*help*;dest=c:/del.cmd" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't create c:/del.cmd"
    estat=1
fi

# should not backup many things, just run the del.cmd command at the end
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
reload
@$out ${cwd}/tmp/log1.out
run job=SystemstateJob storage=File yes
wait
messages
quit
END_OF_DATA

run_bconsole

wget -qO -  "$URL/del_registry_key?key=NewTest$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't remove key NewTest$$ from registry"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=/ storage=File
5
cd @SYSTEMSTATE
mark "Registry Writer"
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

wget -qO $tmp/start.log "$URL/reboot"

# we need a way to start the regress-win32.pl at the start
while ! ping -c 1 $WIN32_ADDR > /dev/null; do
    sleep 5
done
echo "Machine $URL supposedly rebooted"

echo "If the autologon doesn't work, you can double click on c:\\autologon.reg"
i=0
while ! wget -qO /dev/null $URL/nop; do
    sleep 5
    i=`expr $i + 1`
    if [ $i -ge 12 ]; then  # print message every minute
       i=0
       echo "== `date +%T` Waiting for reboot and autologin to $URL, you can also login and start regress-win32.pl by hand"
    fi
done

wget -qO - "$URL/get_registry_key?key=Test$$;val=Val$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Can't find registry key Test$$"
    estat=1
fi

wget -qO - "$URL/get_registry_key?key=NewTest$$;val=Val$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Can't find registry key NewTest$$"
    estat=1
fi

wget -qO - "$URL/get_registry_key?key=RemovedAtRestore$$;val=Val$$" | grep ERR > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should not find registry key RemovedAtRestore$$"
    estat=1
fi

################################################################
# Do a complete restore

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@#
@# now do a complete restore
@#
@$out ${cwd}/tmp/log2.out
restore where=/ storage=File
5
cd @SYSTEMSTATE
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
stop_susan

check_two_logs

wget -qO $tmp/start.log "$URL/reboot"

# we need a way to start the regress-win32.pl at the start
while ! ping -c 1 $WIN32_ADDR > /dev/null; do
    sleep 5
done
echo "Machine $URL supposedly rebooted"

echo "If the autologon doesn't work, you can double click on c:\\autologon.reg"
while ! wget -qO /dev/null $URL/nop; do
    sleep 5
    i=`expr $i + 1`
    if [ $i -ge 12 ]; then  # print message every minute
       i=0
       echo "== `date +%T` Waiting for reboot and autologin to $URL, you can also login and start regress-win32.pl by hand"
    fi
done

wget -qO - "$URL/get_registry_key?key=Test$$;val=Val$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Can't find registry key Test$$"
    estat=1
fi

wget -qO - "$URL/get_registry_key?key=NewTest$$;val=Val$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Can't find registry key NewTest$$"
    estat=1
fi

wget -qO - "$URL/get_registry_key?key=RemovedAtRestore$$;val=Val$$" | grep ERR > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should not find registry key RemovedAtRestore$$"
    estat=1
fi

end_test
